package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomSharedPreferencesUtil;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.CmnBookMerchantAdapter;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.Client;
import com.hemaapp.yjnh.bean.User;

/**通用预约类商家列表
 * @author GuiyouZhang
 * 2015年9月15日
 */
public class CmnBookMerchantListActivity extends BaseActivity implements OnClickListener{

	@Bind(R.id.title_left_btn)
	ImageButton titleLeftBtn;
	@Bind(R.id.title_right_btn)
	Button titleRightBtn;
	@Bind(R.id.title_text)
	TextView titleText;
	@Bind(R.id.layout_default)
	LinearLayout layoutDefault;
	@Bind(R.id.layout_price)
	LinearLayout layoutPrice;
	@Bind(R.id.layout_sales)
	LinearLayout layoutSales;
	@Bind(R.id.listview)
	XtomListView listview;
	@Bind(R.id.refreshLoadmoreLayout)
	RefreshLoadmoreLayout refreshLoadmoreLayout;

    private User user;
	private ArrayList<Client> clients = new ArrayList<Client>();//走走商家列表
	private CmnBookMerchantAdapter adapter;
    private String keytype = "8";//类型	1：首页推荐田家    2：周边田家    3：搜索田家    4：按地区搜索村游商家
    //5：按地区搜索地标商家    6：按地区搜索商家    7：推荐的露营地    8：推荐的房车    9：推荐的景区商家
    private String keyid = "无";
    private String orderby = "1";//1：智能排序    2：热销    3：好评    4：价格排序
    private String ordertype = "1";//1:降序；2:升序
    private String role = "";//商家类型	可选传参数：6乐乐商家；7特产商家；11走走露营商家；
    // 12走走房车商家；13走走景区商家；21苗木商家；22花卉商家；23家电商家；24日用商品商家
    private String lng = "";//可选传参数，需要时传递
    private String lat = "";//纬度	可选传参数，需要时传递
    private int page = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cmn_productions_list);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        user = getApplicationContext().getUser();
        lat = XtomSharedPreferencesUtil.get(mContext, "lat");
        lng = XtomSharedPreferencesUtil.get(mContext, "lng");
        refreshLoadmoreLayout.getOnStartListener().onStartRefresh(refreshLoadmoreLayout);
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_LIST:
			showProgressDialog("请稍候");
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_LIST:
			cancelProgressDialog();
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_LIST:
			String page = netTask.getParams().get("page");
			HemaPageArrayResult<Client> cResult = (HemaPageArrayResult<Client>) baseResult;
			if (cResult == null || cResult.getObjects() == null)
				return;
			if ("0".equals(page)) {// 刷新
                refreshLoadmoreLayout.refreshSuccess();
				clients.clear();
				clients.addAll(cResult.getObjects());
			}else {
                refreshLoadmoreLayout.loadmoreSuccess();
				if (clients.size() > 0)
					this.clients.addAll(cResult.getObjects());
				else {
                    refreshLoadmoreLayout.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			refreshClient(-1);
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_LIST:
            if (page == 0) {// 刷新
                refreshLoadmoreLayout.refreshFailed();
            } else {
                refreshLoadmoreLayout.loadmoreFailed();
            }
            XtomToastUtil.showShortToast(mContext, "获取信息失败" + baseResult.getMsg());
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_LIST:
            if (page == 0) {// 刷新
                refreshLoadmoreLayout.refreshFailed();
            } else {
                refreshLoadmoreLayout.loadmoreFailed();
            }
            XtomToastUtil.showShortToast(mContext, "获取信息失败，请稍后再试");
			refreshClient(failedType);
			break;
		default:
			break;
		}
	}
	
	/**刷新走走商家列表
	 * 
	 */
	private void refreshClient(int failedType) {
		if (null == adapter) {
			adapter = new CmnBookMerchantAdapter(mContext, clients);
			adapter.setEmptyString("暂无商家");
            adapter.setItemClickListener(new OnItemClickListener() {
                @Override
                public void onClickListener(int position) {
                    Intent it = new Intent(mContext, CmnBookMerchantDetailActivity.class);
                    it.putExtra("client_id", clients.get(position).getClient_id());
                    mContext.startActivity(it);
                }
            });
            if (keytype.equals("7"))
                adapter.setType(1);
			listview.setAdapter(adapter);
		}else {
			adapter.setEmptyString("暂无商家");
			adapter.notifyDataSetChanged();
		}
		adapter.setFailtype(failedType);
	}

	@Override
	protected void findView() {
	}

	@Override
	protected void getExras() {
        keytype = mIntent.getStringExtra("keytype");
        keyid = mIntent.getStringExtra("keyid");
        role = mIntent.getStringExtra("role");
        lat = mIntent.getStringExtra("lat");
        lng = mIntent.getStringExtra("lng");
	}


	@Override
	protected void setListener() {
		titleRightBtn.setVisibility(View.GONE);
		titleText.setText(mIntent.getStringExtra("title"));
		refreshLoadmoreLayout.setOnStartListener(new XtomRefreshLoadmoreLayout.OnStartListener() {
			@Override
			public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
				page = 0;
                String token = user == null?"":user.getToken();
                getNetWorker().clientList(token, keytype, keyid, orderby, ordertype, role, page+"", lat, lng);
			}

			@Override
			public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
				page++;
                String token = user == null?"":user.getToken();
                getNetWorker().clientList(token, keytype, keyid, orderby, ordertype, role, page+"", lat, lng);
			}
		});
	}

	@OnClick({R.id.title_left_btn, R.id.layout_default, R.id.layout_price, R.id.layout_sales})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.title_left_btn:
				finish();
				break;
			case R.id.layout_default:
				if (orderby.equals("1")) {
					if (ordertype.equals("1"))
						ordertype = "2";
					else {
						ordertype = "1";
					}
				}else {
                    layoutDefault.setBackgroundColor(Color.parseColor("#ffae40"));
                    layoutPrice.setBackgroundColor(Color.parseColor("#00464a"));
                    layoutSales.setBackgroundColor(Color.parseColor("#00464a"));
					orderby = "1";
				}
				refreshLoadmoreLayout.getOnStartListener().onStartRefresh(refreshLoadmoreLayout);
				break;
			case R.id.layout_price:
				if (orderby.equals("4")) {
					if (ordertype.equals("1"))
						ordertype = "2";
					else {
						ordertype = "1";
					}
				}else {
					layoutPrice.setBackgroundColor(Color.parseColor("#ffae40"));
					layoutDefault.setBackgroundColor(Color.parseColor("#00464a"));
					layoutSales.setBackgroundColor(Color.parseColor("#00464a"));
					orderby = "4";
				}
				refreshLoadmoreLayout.getOnStartListener().onStartRefresh(refreshLoadmoreLayout);
				break;
			case R.id.layout_sales:
				if (orderby.equals("2")) {
					if (ordertype.equals("1"))
						ordertype = "2";
					else {
						ordertype = "1";
					}
				}else {
					layoutSales.setBackgroundColor(Color.parseColor("#ffae40"));
					layoutPrice.setBackgroundColor(Color.parseColor("#00464a"));
					layoutDefault.setBackgroundColor(Color.parseColor("#00464a"));
					orderby = "2";
				}
				refreshLoadmoreLayout.getOnStartListener().onStartRefresh(refreshLoadmoreLayout);
				break;
		}
	}

}
